import streamlit as st
import json

data = None
# 打开并读取JSON文件
with open('state/test.json', 'r', encoding='utf-8') as file:
    str = file.read()
    if str is None or str == '':
        str = "{}"
    data = json.loads(str)

if __name__ == '__main__':
    # 初始化会话状态
    if "counter" not in data:
        data["counter"] = 0
    # 显示当前计数器值
    st.write("计数器值:", data["counter"])

    # 创建一个按钮，并定义回调函数
    if st.button("点击我"):
        data["counter"] = data["counter"] + 1

with open('state/test.json', 'w', encoding='utf-8') as file:
    file.write(json.dumps(data))
    file.flush()
